package com.zl.config;

import com.zl.pojo.User;
import com.zl.service.UserService;
import lombok.experimental.Accessors;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class UserRealm extends AuthorizingRealm {
    @Accessors
    UserService userService;
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权");
        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证");
        /*
        //使用内存用户名密码
        String name="admin";
        String pwd="123456";

        UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;
        if (!userToken.getUsername().equals(name)){
            return null;
        }
        return new SimpleAuthenticationInfo("",pwd,"");*/


        //数据库中获取用户名
        UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;
        User user = userService.queryUserByName(userToken.getUsername());
        if (user==null){
            return null;
        }
        //判断密码  Shiro自己做,不经过程序员
        return new SimpleAuthenticationInfo("",user.getPwd(),"");


    }
}
